package com.gzhryc.shared_device.oem.micro.api.controllers.callback;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.pay.adaptation.alipay.DefaultAlipayConfig;
import com.gzhryc.pay.adaptation.alipay.enums.EAlipayAdvanceNotifyType;
import com.gzhryc.pay.adaptation.alipay.msg.advance_pay.AlipayAdvancePayConfirmNotify;
import com.gzhryc.pay.adaptation.alipay.msg.normal.AlipayNormalPayNotify;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.EOperatorType;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.oem.code.PayServiceFactory;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.micro.api.MicroConstants;
import com.gzhryc.shared_device.oem.micro.api.OrderCollectManager;

public class AlipayCallbackController {

	static Logger log = Logger.getLogger(AlipayCallbackController.class);

	@Route(value = {"/paycall/alipay/{appId}/{busiType}/pay","/paycall/{busiType}/alipay/{appId}/pay"})
	public String wxpay(HttpServletRequest request, HttpServletResponse response) {
		String appId = WebUtils.getString(request, "appId");
		String busiType = WebUtils.getString(request, "busiType",EBusinessType.CDCZ.index());
		try {
			Map<String, String> params = WebUtils.getSingleParameter(request);
			DefaultAlipayConfig config = PayServiceFactory.self().getAlipayConfig(appId,busiType.toUpperCase());
			if(config != null) {
				AlipayNormalPayNotify payNotify = new AlipayNormalPayNotify(params, config.getAlipayPublicKey(),appId);
			
				OperateInfo operateInfo = new OperateInfo(MicroConstants.PROGRAM_TYPE,MicroConstants.PROGRAM_TYPE_NAME);
				operateInfo.setOperator(EOperatorType.System.index(),"支付宝支付回调接口");
				operateInfo.setOperateNote("支付宝支付回调");
				OrderCollectManager.payResult(payNotify, operateInfo);
			}
		} catch (Exception e) {
			log.error(appId + "支付宝支付回调错误，" + e.getMessage(), e);
		}
		return "text:SUCCESS";
	}

	@Route(value = {"/paycall/alipay/{appId}/{busiType}/rentbill","/paycall/{busiType}/alipay/{appId}/rentbill"})
	public String rentBill(HttpServletRequest request, HttpServletResponse response) {
		String appId = WebUtils.getString(request, "appId");
		String busiType = WebUtils.getString(request, "busiType",EBusinessType.CDCZ.index());
		OperateInfo operateInfo = new OperateInfo(MicroConstants.PROGRAM_TYPE, MicroConstants.PROGRAM_TYPE_NAME);
		operateInfo.setOperator(EOperatorType.System.index(), "支付宝免押回调接口");
		try {
			Map<String, String> params = WebUtils.getSingleParameter(request);
			DefaultAlipayConfig config = PayServiceFactory.self().getAlipayConfig(appId,busiType.toUpperCase());
			if(config != null) {
				String notify_type = params.get("notify_type");
				if (StringTools.isNotBlank(notify_type)) {
					if (EAlipayAdvanceNotifyType.fund_auth_freeze.index().equals(notify_type)) {
						AlipayAdvancePayConfirmNotify confirmNotify = new AlipayAdvancePayConfirmNotify(params, config.getAlipayPublicKey(),appId);
						operateInfo.setOperateNote("支付宝预支付用户确认");
						OrderCollectManager.payResult(confirmNotify, operateInfo);
					}else if (EAlipayAdvanceNotifyType.fund_auth_unfreeze.index().equals(notify_type)) {
						
					}
				}
			}
		} catch (Exception e) {
			log.error(appId + "微信免押回调错误", e);
		}
		return "text:SUCCESS";
	}
}
